<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Scopes\IsDelScope;

class OrderModel extends BaseModel {

    protected static function booted()
    {
        parent::booted();
    }

    protected $table = "dtrd_ent_shc_rd_order";
    protected $primaryKey = "data_id";
    public $incrementing = false;
    protected $keyType = 'string';

    const CREATED_AT = 'create_time';
    const UPDATED_AT = null;
    protected $connection = 'oms_prod';

    protected $casts = [
        'order_info_paystatus' => 'boolean',
        'create_time' => 'datetime:Y-m-d H:i:s',
    ];

    protected $appends = [
        'order_info_delivery_type_text',
        'order_info_paystatus_text',
        'order_info_orderstatus_text',
    ];

    protected $hidden = ['is_del'];
    protected $fillable = [
        'data_id',
        'order_info_userid',
        'order_info_title',
        'order_info_no',
        'order_info_sum',
        'order_info_payment',
        'order_info_paystatus',
        'order_info_orderstatus',
        'pay_time',
        'complete_time',
        'order_info_type',
        'order_info_service_id',
        'order_info_qrcode',
        'order_info_qrcode_result',
        'order_info_pay_type',
        'transaction_id',
        'remark',
        'order_info_delivery_type',
        'order_info_buy_type',
        'org_code',
        'order_info_product_category'
    ];

    public function getOrderInfoDeliveryTypeTextAttribute()
    {
        switch ($this->order_info_delivery_type) {
            case 1:
                return '线下领取';
            case 2:
                return '配送到家';
            default:
                return '线下领取';
        }
    }

    public function getOrderInfoPaystatusTextAttribute()
    {
        switch ($this->order_info_paystatus) {
            case 0:
                return '待支付';
            case 1:
                return '已支付';
            default:
                return '待支付';
        }
    }

    public function getOrderInfoOrderstatusTextAttribute()
    {
        switch ($this->order_info_orderstatus) {
            case 1:
                return '待支付';
            case 2:
                return '已取消';
            case 3:
                return '已支付';
            case 4:
                return '已完成';
            case 5:
                return '已领取';
            case 6:
                return '退款中';
            case 7:
                return '已退款';
            case 8:
                return '已发货';
            case 9:
                return '拒绝退款';
            default:
                return '';
        }
    }

    public function address()
    {
        return $this->hasOne(OrderAddressModel::class, 'order_id', 'data_id');
    }

    public function products()
    {
        return $this->hasMany(OrderProductModel::class, 'order_id', 'data_id');
    }
}
